﻿using System;
using System.Collections.Generic;
using System.Text;
using Cosmos.IL2CPU.IL.CustomImplementations.System;
using Cosmos.IL2CPU.Plugs;
using Sys = Cosmos.System;
 
 
 
namespace Optimus
{
    public class Kernel : Sys.Kernel
    {
        public static uint count = 0;
        public static string xResult = "";
        public static Cosmos.Core.INTs.TSS KernelStack;
        //Create a TSS structure
        //TSS structure is for Hardware multitasking
        //Finish optimising system call Int 0x80

        //Free Memory 0x500-0x7BFF 30kB
        //            0x80000-0x9FBFF - 480.5 kB

        static uint d = 0;

        protected override void BeforeRun()
        {
 
            Core.MemoryManager.Init();
            Console.WriteLine("[ ****** Optimus Operating System ****** ]");
            Console.WriteLine("Version Type: Multitasking 2");
        }    

        protected override void Run()
        {
 
            Console.Write(">CMD# ");
            xResult = Console.ReadLine();

            if (xResult == "desktop")
            {
                VGA.SetMode(VGA.ScreenResolution.Res320x480x8);
                VGA.Clear(0);
                unsafe
                {
                    for (int y = 0; y < VGA.ScreenHeight; y++)
                    {
                        for (int x = 0; x < VGA.ScreenWidth; x++)
                        {

                            VGA.dbuffer[(y * VGA.ScreenWidth) + x] = 0;
                        }

                    }
                    Desktop.DrawDesktop();
                }
            }
            else if (xResult == "pci")
            {
                int per = 0;
                int max = 65536;
                Console.Write("Detecting PCI Devices...");
                for (int x = 0; x < 256; x++)
                {
                    for (int y = 0; y < 32; y++)
                    {
                        PCI.FindPCIDevices((byte)x, (byte)y);
                        per = ((x * y) * 8);
                        Console.CursorLeft = 24;
                        Console.CursorTop = 3;
                        Console.Write(per.ToString() + "/65536");
                    }
                    Console.CursorLeft = 24;
                    Console.CursorTop = 3;
                    Console.Write(per.ToString() + "/65536");
                }
                Console.CursorLeft = 35;
                Console.CursorTop = 3;
                Console.WriteLine("-OK");
                PCI.GetNames();
                int f=0;
                    Console.WriteLine("*******************************");
                    for (int i = 0; i < max; i++)
                    {
                        if (PCI.PCIDeviceList[i].VendorID != 0xFFFF & PCI.PCIDeviceList[i].VendorID != 0)
                        {
                            Console.WriteLine("Class: " + PCI.PCIDeviceList[i].ClassCode.ToString() + " SubClass: " + PCI.PCIDeviceList[i].SubClass.ToString());
                            f++;
                            if (f == 10)
                            {
                                f = 0;
                                Console.ReadKey();
                                Console.Clear();
                                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                                Console.WriteLine("Version Type: Multitasking");
                            }
                        }
                    }

                 
            
           
            }
            else if (xResult == "cls")
            {
                Console.Clear();
                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                Console.WriteLine("Version Type: Multitasking");
                
            }
            else if (xResult == "memx")
            {
                Core.MemoryMonitor aMonitor = new Core.MemoryMonitor();
                aMonitor.InitEx();
                aMonitor.Monitor();
            }

            else if (xResult == "ktask")
            {
                Manager.Kx.InitEx();
                //Multitasking Test
                Console.Clear();
                unsafe
                {
                    Manager.CreateTask(0, Manager.CodeToRun0);
                    Manager.CreateTask(1, Manager.CodeToRun1);
                    Manager.CreateTask(2, Manager.CodeToRun2);
                    Manager.Init();
                }

            }
            else if (xResult == "ktask2")
            {
                //Multitasking Test
                Console.Clear();
                unsafe
                {
                    Manager.CreateUserTask(0, Manager.CodeToRun0);
                    Manager.CreateUserTask(1, Manager.CodeToRun0);
                    Manager.CreateUserTask(2, Manager.CodeToRun0);
                    Manager.Init();
                }

            }

            else if (xResult == "atatest")
            {
                Console.Clear();
                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                Console.WriteLine("Version Type: Multitasking");
              //  ATA.Init();
               // ATA.ReadBlock(0);
               // ATA.PrintBuffer();
            }
            else if (xResult == "atapitest")
            {
                Console.Clear();
                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                Console.WriteLine("Version Type: Multitasking");
                ATAPI.Init();
                ATAPI.ReadBlock(1);
                ATAPI.PrintBuffer();
            }
            else if (xResult == "fddtest")
            {
                Console.Clear();
                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                Console.WriteLine("Version Type: Multitasking");
                FDD.Init(true);
                FDD.ReadBlock(0, 0, 1, 1);
                FDD.PrintBuffer();
            }
            else if (xResult == "thisisanusedinstruction")
            {
                //This just tricks compiler into adding it to kernel
                Manager.scheduler(0);
                Manager.IdleTask();
            }
            else if (xResult == "reboot")
            {
                Console.Clear();
                Console.CursorLeft = 10;
                Console.CursorTop = 10;
                Console.WriteLine("Rebooting your machine");
                Cosmos.Core.Bootstrap.CPU.UpdateIDT(false);
                Cosmos.Core.Bootstrap.CPU.Reboot();
            }
            else if (xResult == "help")
            {
                Console.Clear();
                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                Console.WriteLine("Version Type: Multitasking");
                Console.WriteLine("help------view list of commands.");
                Console.WriteLine("memx------view memory information.");
                Console.WriteLine("pci-------check bus for pci devices.");
                Console.WriteLine("desktop---perform a vga test.");
                Console.WriteLine("cls-------clean the screen.");
                Console.WriteLine("ktask-----perform multitasking test.");
                Console.WriteLine("atatest---perform ata pio test.");
                Console.WriteLine("atapitest-perform atapi with irq test.");
                Console.WriteLine("fddtest---perform fdd pio test.");
                Console.WriteLine("reboot----perform a vga test.");
            }
            else
            {
                Console.Clear();
                Console.WriteLine("[ ****** Optimus Operating System ****** ]");
                Console.WriteLine("Version Type: Multitasking");
                Console.WriteLine("Invalid CMD: " + xResult);
            }
        }
     
    }
}